<template>
  <div class="index-group-box">
    <span class="sku-title">{{ title }}</span>
    <div ref="groupBoxRef" class="index-group-boxIn">
      <template v-if="type === 'image'">
        <div>
          <img
            v-for="(item, index) in dataList"
            :key="'thumb-' + index"
            :src="item.skuImageUrl"
            :alt="'Thumbnail ' + index"
            :class="{
              'group-card-s': index === currentSelectedIndex,
              'group-card-n': index !== currentSelectedIndex
            }"
            ref="groupCardRef"
            @click="onItemClick(index, item)"
          />
        </div>
      </template>
      <template v-else-if="type === 'text'">
        <div class="sku-text-container">
          <span
            v-for="(item, index) in dataList"
            :key="index"
            @click="onItemClick(index, item)"
            :class="{
              'sku-item-text-s': index === currentSelectedIndex,
              'sku-item-text-n': index !== currentSelectedIndex
            }"
            >{{ item?.attrVauleTrans || '' }}</span
          >
        </div>
      </template>
      <template v-else-if="type === 'num'">
        <el-input-number
          v-model="currentNum"
          :step="1"
          :min="1"
          :max="9999"
          class="sku-input"
          @change="numValueChange"
        ></el-input-number>
      </template>
    </div>
  </div>
</template>

<script setup lang="ts">
import { defineProps, ref } from 'vue'

const props = defineProps({
  title: {
    type: String,
    required: true
  },
  items: {
    type: Array,
    required: false
  },
  type: {
    type: String,
    required: true
  },
  currentIndex: {
    type: Number
  }
})

const dataList = ref<any[]>(props.items || [])
const currentSelectedIndex = ref(0)
const currentNum = ref(1)
const groupBoxRef = ref<HTMLElement | null>(null)
const didSelectIndexEmits = defineEmits(['onSkuClick'])

const numValueChange = (value: number) => {
  onItemClick(value, null)
}

const onItemClick = (value: number, item: any) => {
  currentSelectedIndex.value = value
  if (props.type === 'num') {
    didSelectIndexEmits('onSkuClick', {
      type: props.type,
      value: value
    })
  } else {
    didSelectIndexEmits('onSkuClick', {
      type: props.type,
      index: value,
      value: item
    })
  }
}
</script>

<style lang="scss" scoped>
.index-group-box {
  display: flex;
  justify-content: start;
  width: 100%;

  :deep(.sku-input.el-input-number) {
    .el-input-number__increase,
    .el-input-number__decrease {
      color: black;
      font-weight: 600;
      background-color: white;
    }

    .el-input__inner {
      color: black;
      font-size: 14px;
      font-weight: 600;
      border: 0;
    }
  }
}

.index-group-boxIn {
  display: flex;
  flex: 1;
}

.group-card-n,
.group-card-s {
  margin-right: 16px;
  width: 40px;
  height: 40px;
  border-radius: 4px;
  border: 1px solid #e5e5e5;
  cursor: pointer;
}

.group-card-s {
  border-width: 2px;
  border-color: #fa4756;
}

.sku-title {
  margin-left: 24px;
  margin-right: 16px;
  margin-top: 8px;
  font-size: 14px;
  font-weight: 400;
  color: #333333;
  max-width: 120px;
  min-width: 40px;
}

.sku-item-text-s,
.sku-item-text-n {
  //height: 32px;
  padding: 5px 8px;
  font-size: 16px;
  color: #999999;
  border-radius: 4px;
  border: 1px solid #e5e5e5;
  cursor: pointer;
}

.sku-item-text-s {
  color: #fa4756;
  border: 1px solid #fa4756;
}

.sku-text-container {
  display: flex;
  flex-direction: row;
  justify-content: start;
  align-items: center;
  flex-wrap: wrap; /* 添加 flex-wrap 属性以允许子视图换行 */
  gap: 16px; /* 设置项目之间的间隔为16px */
}
</style>
